package optiondialoges;

import javax.swing.JPanel;
import java.awt.Frame;
import java.awt.BorderLayout;
import javax.swing.JDialog;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JRadioButton;

import panel.GlyphPanel;


import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.Serializable;

public class ButterworthOptionDialog extends JDialog implements ActionListener,
		WindowListener, Serializable {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JPanel buttonPanel = null;
	private JPanel optionPanel = null;
	private JButton okButton = null;
	private JButton cancelButton = null;
	private JLabel jLabel = null;
	private JTextField orderTextField = null;
	private JLabel jLabel1 = null;
	private JTextField thresholdTextField = null;
	private JLabel jLabel2 = null;
	private JTextField sizeTextField = null;
	private JRadioButton lowRadioButton = null;
	private JRadioButton highRadioButton = null;
	private GlyphPanel panel;
	private String order;
	private String cutoff;
	private String size;
	private boolean low;

	/**
	 * @param owner
	 */
	public ButterworthOptionDialog(Frame owner, GlyphPanel panel) {
		super(owner);
		this.panel = panel;
		this.addWindowListener(this);
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(264, 231);
		this.setContentPane(getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getButtonPanel(), BorderLayout.SOUTH);
			jContentPane.add(getOptionPanel(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes buttonPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getButtonPanel() {
		if (buttonPanel == null) {
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.insets = new Insets(0, 10, 10, 10);
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 1;
			gridBagConstraints.insets = new Insets(0, 10, 10, 10);
			gridBagConstraints.gridy = 0;
			buttonPanel = new JPanel();
			buttonPanel.setLayout(new GridBagLayout());
			buttonPanel.add(getOkButton(), gridBagConstraints1);
			buttonPanel.add(getCancelButton(), gridBagConstraints);
		}
		return buttonPanel;
	}

	/**
	 * This method initializes optionPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getOptionPanel() {
		if (optionPanel == null) {
			GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
			gridBagConstraints9.gridx = 1;
			gridBagConstraints9.anchor = GridBagConstraints.WEST;
			gridBagConstraints9.gridy = 4;
			GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
			gridBagConstraints8.gridx = 1;
			gridBagConstraints8.anchor = GridBagConstraints.WEST;
			gridBagConstraints8.gridy = 3;
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints7.gridy = 2;
			gridBagConstraints7.weightx = 1.0;
			gridBagConstraints7.anchor = GridBagConstraints.WEST;
			gridBagConstraints7.insets = new Insets(10, 0, 0, 0);
			gridBagConstraints7.gridx = 1;
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.gridx = 0;
			gridBagConstraints6.anchor = GridBagConstraints.EAST;
			gridBagConstraints6.insets = new Insets(10, 10, 0, 10);
			gridBagConstraints6.gridy = 2;
			jLabel2 = new JLabel();
			jLabel2.setText("Size");
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints5.gridy = 1;
			gridBagConstraints5.weightx = 1.0;
			gridBagConstraints5.anchor = GridBagConstraints.WEST;
			gridBagConstraints5.insets = new Insets(10, 0, 0, 0);
			gridBagConstraints5.gridx = 1;
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.gridx = 0;
			gridBagConstraints4.anchor = GridBagConstraints.EAST;
			gridBagConstraints4.insets = new Insets(10, 10, 0, 10);
			gridBagConstraints4.gridy = 1;
			jLabel1 = new JLabel();
			jLabel1.setText("Threshold factor");
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints3.gridy = 0;
			gridBagConstraints3.weightx = 1.0;
			gridBagConstraints3.anchor = GridBagConstraints.WEST;
			gridBagConstraints3.insets = new Insets(10, 0, 0, 0);
			gridBagConstraints3.gridx = 1;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 0;
			gridBagConstraints2.anchor = GridBagConstraints.EAST;
			gridBagConstraints2.insets = new Insets(10, 10, 0, 10);
			gridBagConstraints2.gridy = 0;
			jLabel = new JLabel();
			jLabel.setText("Order");
			optionPanel = new JPanel();
			optionPanel.setLayout(new GridBagLayout());
			optionPanel.add(jLabel, gridBagConstraints2);
			optionPanel.add(getOrderTextField(), gridBagConstraints3);
			optionPanel.add(jLabel1, gridBagConstraints4);
			optionPanel.add(getThresholdTextField(), gridBagConstraints5);
			optionPanel.add(jLabel2, gridBagConstraints6);
			optionPanel.add(getSizeTextField(), gridBagConstraints7);
			optionPanel.add(getLowRadioButton(), gridBagConstraints8);
			optionPanel.add(getHighRadioButton(), gridBagConstraints9);
			ButtonGroup group = new ButtonGroup();
			group.add(getLowRadioButton());
			group.add(getHighRadioButton());
		}
		return optionPanel;
	}

	/**
	 * This method initializes okButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getOkButton() {
		if (okButton == null) {
			okButton = new JButton();
			okButton.setText("Ok");
			okButton.addActionListener(this);
		}
		return okButton;
	}

	/**
	 * This method initializes cancelButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getCancelButton() {
		if (cancelButton == null) {
			cancelButton = new JButton();
			cancelButton.setText("Cancel");
			cancelButton.addActionListener(this);
		}
		return cancelButton;
	}

	/**
	 * This method initializes orderTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getOrderTextField() {
		if (orderTextField == null) {
			orderTextField = new JTextField();
			orderTextField.setPreferredSize(new Dimension(120, 20));
			orderTextField.setText("1");
		}
		return orderTextField;
	}

	/**
	 * This method initializes thresholdTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getThresholdTextField() {
		if (thresholdTextField == null) {
			thresholdTextField = new JTextField();
			thresholdTextField.setPreferredSize(new Dimension(120, 20));
			thresholdTextField.setText("50");
		}
		return thresholdTextField;
	}

	/**
	 * This method initializes sizeTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getSizeTextField() {
		if (sizeTextField == null) {
			sizeTextField = new JTextField();
			sizeTextField.setPreferredSize(new Dimension(120, 20));
			sizeTextField.setText("512");
		}
		return sizeTextField;
	}

	/**
	 * This method initializes lowRadioButton
	 * 
	 * @return javax.swing.JRadioButton
	 */
	private JRadioButton getLowRadioButton() {
		if (lowRadioButton == null) {
			lowRadioButton = new JRadioButton();
			lowRadioButton.setText("Low Pass Filter");
			lowRadioButton.setSelected(true);
		}
		return lowRadioButton;
	}

	/**
	 * This method initializes highRadioButton
	 * 
	 * @return javax.swing.JRadioButton
	 */
	private JRadioButton getHighRadioButton() {
		if (highRadioButton == null) {
			highRadioButton = new JRadioButton();
			highRadioButton.setText("High Pass Filter");
		}
		return highRadioButton;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		JButton button = (JButton) e.getSource();
		if (button.getText().contentEquals("Ok")) {
			order=orderTextField.getText();
			cutoff=thresholdTextField.getText();
			size=sizeTextField.getText();
			low=lowRadioButton.isSelected();
			String command;
			if(low)
			command=String.format("run(\"Butterworth Filters\", \"%s %s %s 0\");%n", order,cutoff, size);
			else
				command=String.format("run(\"Butterworth Filters\", \"%s %s %s 1\");%n", order,cutoff, size);
			panel.setCommand(command);
			panel.setOptionsSet(true);
			panel.setDialog(this);
		}else{
			orderTextField.setText(order);
			thresholdTextField.setText(cutoff);
			sizeTextField.setText(size);
			lowRadioButton.setSelected(low);
		}
		this.setVisible(false);

	}

	@Override
	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowClosed(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowClosing(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowDeactivated(WindowEvent e) {
		orderTextField.setText(order);
		thresholdTextField.setText(cutoff);
		sizeTextField.setText(size);
		lowRadioButton.setSelected(low);

	}

	@Override
	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowOpened(WindowEvent e) {
		order=orderTextField.getText();
		cutoff=thresholdTextField.getText();
		size=sizeTextField.getText();
		low=lowRadioButton.isSelected();

	}

} // @jve:decl-index=0:visual-constraint="10,-9"
